/**
 * Copyright 2008-2009. Chongqing Communications Industry Services Co.,Ltd Information Technology Branch. All rights
 * reserved. <a>http://www.cqcis.com</a>
 */
package com.cqcis.uip.base.filelog.stream.impl;
import java.io.File;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

import com.cqcis.uip.base.framework.service.StreamOutput;

/**
 * @author huangmy
 * @date 2007-11-24
 */
public class SocketStreamOutput implements StreamOutput {
	public String write(String filePath, String fileName, ByteBuffer buffer) throws Exception {
		int oldPosition = buffer.position();
		File file = new File(filePath);

		if (!file.exists()) {
			file.mkdirs();
		}

		file = new File(filePath + fileName);

		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream(file);
			FileChannel channel = fos.getChannel();
			channel.write(buffer); 
		} finally {
			buffer.position(oldPosition);
			
			if (fos != null) {
				fos.close();
			}
		}

		return file.getAbsolutePath();
	}
}